/*******************************************************************************
 * Copyright 2008 www.softao.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
/**
 * Place holder for copyright, needs to be replaced before release.
 */
package test.com.softao.ming.gwt.client.drawing2d;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;
import com.softao.ming.gwt.drawing2d.client.ui.Canvas;
import com.softao.ming.gwt.drawing2d.client.ui.Graphics;

/**
 * 
 */
public class BasicDrawingForm extends Composite {
	private final FlexTable pnlLayout;
	private final Button btnDraw;
	private final Canvas mCanvas;

	/**
	 * Initializes a new instance of <code>BasicDrawingForm</code>.
	 */
	public BasicDrawingForm() {
		this.pnlLayout = new FlexTable();
		this.btnDraw = new Button("Draw");
		this.btnDraw.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				draw();
			}
		});
		this.mCanvas = new Canvas(300, 300);

		this.layoutPanel();
		this.initWidget(this.pnlLayout);
	}

	private void layoutPanel() {
		this.pnlLayout.setWidget(0, 0, this.btnDraw);
		this.pnlLayout.setWidget(1, 0, this.mCanvas);
	}

	private void draw() {
		Graphics g = this.mCanvas.getGraphics();
		g.setFillStyle("green");
		g.fillRect(5, 5, 20, 20);
		g.setStrokeStyle("red");
		g.strokeRect(20, 20, 25, 25);

		g.beginPath();
		g.setLineWidth(1);
		g.moveTo(1, 1);
		g.lineTo(80, 100);
		g.lineTo(100, 20);
		g.closePath();
		g.stroke();

		g.setStrokeStyle("blue");
		g.setFillStyle("black");
		g.beginPath();
		g.moveTo(120, 50);
		g.lineTo(150, 70);
		g.lineTo(150, 50);

		g.quadraticCurveTo(150, 150, 80, 80);
		g.bezierCurveTo(85, 25, 75, 37, 75, 40);
		g.closePath();
		g.fill();

		g.beginPath();
		g.rect(180, 180, 80, 80);
		g.rect(200, 200, 80, 80);
		g.stroke();

		g.beginPath();
		g.arc(200, 100, 20, 0, (float) Math.PI, true);
		g.stroke();

		g.save();
		g.translate(150, 0);

		g.fillRect(0, 0, 150, 150);
		g.setFillStyle("#09F");
		g.fillRect(15, 15, 120, 120);
		g.setFillStyle("#FFF");
		g.setGlobalAlpha(0.5F);
		g.fillRect(30, 30, 90, 90);
		g.fillRect(45, 45, 60, 60);
		g.fillRect(60, 60, 30, 30);

		g.restore();
		g.save();
		g.translate(10, 140);

		g.setFillStyle("#FD0");
		g.fillRect(0, 0, 75, 75);
		g.setFillStyle("#6C0");
		g.fillRect(75, 0, 75, 75);
		g.setFillStyle("#09F");
		g.fillRect(0, 75, 75, 75);
		g.setFillStyle("#F30");
		g.fillRect(75, 75, 75, 75);
		g.setFillStyle("#FFF");

		g.setGlobalAlpha(0.2F);

		for (int i = 0; i < 7; i++) {
			g.beginPath();
			g.arc(75F, 75F, 10F + (10 * i), 0F, (float) Math.PI * 2, true);
			g.fill();
		}

		g.restore();

		g.beginPath();
		g.arc(200, 200, 20, 0, (float) Math.PI * 2, true);
		g.stroke();

		g.save();
		g.setGlobalAlpha(1.0F);
		// g.translate(75, 75);
		
		for (int i = 1; i < 6; i++) { // Loop through rings (from inside to out)
			g.save();
			g.setFillStyle("rgb(" + (51 * i) + "," + (255 - 51 * i) + ",255");

			for (int j = 0; j < i * 6; j++) { // draw individual dots
				g.rotate((float) (Math.PI * 2 / (i * 6)));
				g.beginPath();
				// ctx.arc(0,i*12.5,5,0,Math.PI*2,true);
				g.rect(0F, i * 12.5F, 5F, 5F);
				g.fill();
			}
			g.restore();
		}
		g.restore();
	}
}
